import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import type { PatientInfo, PatientQueryParams } from '@/types/patient';
import { getPatientList } from '@/api/patient';

export const usePatientStore = defineStore('patient', () => {
  // 状态
  const patientList = ref<PatientInfo[]>([]);
  const loading = ref(false);
  const error = ref<string | null>(null);
  const currentParams = ref<PatientQueryParams>({});

  // 计算属性
  const hasPatients = computed(() => patientList.value.length > 0);
  
  // 男性患者
  const malePatients = computed(() => 
    patientList.value.filter(patient => patient.patientGender === '1')
  );
  
  // 女性患者
  const femalePatients = computed(() => 
    patientList.value.filter(patient => patient.patientGender === '0')
  );
  
  // 会员患者
  const memberPatients = computed(() => 
    patientList.value.filter(patient => patient.isMember === '1')
  );

  // 方法
  const fetchPatientList = async (params?: PatientQueryParams) => {
    try {
      loading.value = true;
      error.value = null;
      currentParams.value = params || {};

      const response = await getPatientList(params);
      
      if (response.ok && response.code === '200') {
        patientList.value = response.data;
      } else {
        throw new Error(response.msg || '获取患者列表失败');
      }
    } catch (err) {
      error.value = err instanceof Error ? err.message : '获取患者列表失败';
      //console.error('获取患者列表失败:', err);
    } finally {
      loading.value = false;
    }
  };

  // 搜索患者
  const searchPatients = async (keyword: string) => {
    await fetchPatientList({ ...currentParams.value, keyword });
  };

  // 按性别筛选
  const filterByGender = async (gender: string) => {
    await fetchPatientList({ ...currentParams.value, gender });
  };

  // 按会员状态筛选
  const filterByMember = async (isMember: string) => {
    await fetchPatientList({ ...currentParams.value, isMember });
  };

  // 根据ID查找患者
  const findPatientById = (patientId: number): PatientInfo | undefined => {
    return patientList.value.find(patient => patient.patientId === patientId);
  };

  // 获取患者年龄显示文本
  const getPatientAge = (patient: PatientInfo): string => {
    if (patient.ageYear > 0 && patient.ageMonth > 0) {
      return `${patient.ageYear}岁${patient.ageMonth}个月`;
    } else if (patient.ageYear > 0) {
      return `${patient.ageYear}岁`;
    } else if (patient.ageMonth > 0) {
      return `${patient.ageMonth}个月`;
    }
    return '年龄未知';
  };

  // 获取性别显示文本
  const getGenderText = (gender: string): string => {
    return gender === '1' ? '男' : '女';
  };

  // 获取会员状态显示文本
  const getMemberText = (isMember: string): string => {
    return isMember === '1' ? '会员' : '普通';
  };

  // 清除数据
  const clearPatientData = () => {
    patientList.value = [];
    error.value = null;
    currentParams.value = {};
  };

  // 刷新数据
  const refreshPatientList = () => {
    return fetchPatientList(currentParams.value);
  };

  return {
    // 状态
    patientList,
    loading,
    error,
    currentParams,
    
    // 计算属性
    hasPatients,
    malePatients,
    femalePatients,
    memberPatients,
    
    // 方法
    fetchPatientList,
    searchPatients,
    filterByGender,
    filterByMember,
    findPatientById,
    getPatientAge,
    getGenderText,
    getMemberText,
    clearPatientData,
    refreshPatientList
  };
});
